package com.kolon.business.mConcise.controller;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringEscapeUtils;
import org.jdom.Document;
import org.jdom.Element;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.kolon.framework.constant.Constant;
import com.kolon.framework.interfaces.SimpleController;
import com.kolon.framework.util.ComUtil;
import com.kolon.framework.util.MsgUtil;

@Controller("com.kolon.business.mKolonNews.controller.MConciseListController")
public class MConciseListController implements SimpleController {

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping(value="/business/mConcise/mConciseList.do")
	public ModelAndView controller(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		// 도메인
		String url =  MsgUtil.getMessage("domain.url");
		
		if(url != null)System.out.println("URL"+url);
		// 세션
		HttpSession httpSession = request.getSession();
		Map userMap = (Map)httpSession.getAttribute(Constant.SESSION_USER_INFO);
		String id = (String)userMap.get("userId");
		String pw = (String)userMap.get("userPw");		
		String conciseGrade = (String)userMap.get("conciseGrade"); //콘사이스 권한 등급
		
		String type = ComUtil.nullChk(request.getParameter("type"), "TopNews"); //TopNews , CeoInfo, Manage
		String page = ComUtil.nullChk(request.getParameter("page"), "1");


		
		//검색 관련
		String SearchTitle = request.getParameter("SearchTitle");
		String SearchFirst     = ComUtil.nullChk(request.getParameter("SearchFirst"));
		String SearchStartDate = ComUtil.nullChk(request.getParameter("SearchStartDate"),"");
		String SearchEndDate   = ComUtil.nullChk(request.getParameter("SearchEndDate"),"");
		
		
		
		Map map = new HashMap();
		// XML 데이터 
		//map = new HashMap();
		//map.put("BoardCD", element.getValue());
		ModelAndView modelAndView = new ModelAndView();
		//modelAndView.addObject("map", map);
		StringBuffer buffer2 = new StringBuffer();
		buffer2.append(url);
		buffer2.append("/XMLParser/mConcise_xml.jsp?mode=").append("2");
		buffer2.append("&Type=").append(type);//417438
		buffer2.append("&Grade=").append(conciseGrade);
		
		//검색 항목 시작
		buffer2.append("&SearchTitle=").append(ComUtil.getEncode(request.getParameter("SearchTitle")==null ? "" : SearchTitle,"UTF-8"));
		buffer2.append("&SearchFirst=").append(SearchFirst);
		buffer2.append("&SearchStartDate=").append(SearchStartDate);
		buffer2.append("&SearchEndDate=").append(SearchEndDate);
		//검색 항목 종료
		
		buffer2.append("&Page=").append(page);
		buffer2.append("&ViewRowCount=").append("15");
		buffer2.append("&userid=").append(id);
		buffer2.append("&pw=").append(pw);
		
		System.out.println(buffer2.toString());
		// XML DOCUMENT 및 인증 처리
		Document doc2 = ComUtil.getDocument(buffer2.toString(), id, pw);
		Element element2 = doc2.getRootElement();
		List xmlList2 = element2.getChildren("BoardList");
		Iterator itXml2 = xmlList2.iterator();
		List mapList2 = new ArrayList();
		// XML 데이터 
		while (itXml2.hasNext()) {
			Element el = (Element)itXml2.next();
			map = new HashMap();
			map.put("ArticleCD", el.getChild("ArticleCD").getText());
			map.put("UserName", el.getChild("UserName").getText());
			//map.put("Title", ComUtil.subString(StringEscapeUtils.unescapeXml(el.getChild("Title").getText()), 15));
			map.put("Title", StringEscapeUtils.unescapeXml(el.getChild("Title").getText()));
			map.put("UCreateDate", el.getChild("UCreateDate").getText());
			map.put("Importance", el.getChild("Importance").getText());
			map.put("InfoGrade", el.getChild("InfoGrade").getText());		
			
			mapList2.add(map);
		}
		// TableInfo 영역
		Document doc3 = ComUtil.getDocument(buffer2.toString(), id, pw);
		Element element3 = doc3.getRootElement();
		List xmlList3 = element3.getChildren("TableInfo");
		Iterator itXml3 = xmlList3.iterator();
		// XML 데이터 
		if (itXml3.hasNext()) {
			Element el = (Element)itXml3.next();
			map = new HashMap();
			map.put("TotalRecordCnt", el.getChild("TotalRecordCnt").getText());
			map.put("CurrPage", el.getChild("CurrPage").getText());
			map.put("ViewRowCount", el.getChild("ViewRowCount").getText());
			map.put("CurrCnt", el.getChild("CurrCnt").getText());
		}
		// 리턴
		modelAndView.addObject("mapList2", mapList2);
		modelAndView.addObject("map", map);
		modelAndView.addObject("page", ComUtil.sToi(page)+1); //현재 page + 1
		//modelAndView.addObject("ScrollTo", (ComUtil.sToi(page)*15)-15); //현재 page + 1
		modelAndView.addObject("type", type);
		modelAndView.addObject("more", ComUtil.nullChk(request.getParameter("more"), "n")); //현재 page + 1

		
		modelAndView.addObject("SearchTitle",SearchTitle);
		modelAndView.addObject("SearchFirst",SearchFirst);
		modelAndView.addObject("SearchStartDate",SearchStartDate);
		modelAndView.addObject("SearchEndDate",SearchEndDate);
		
		return modelAndView;
	}

}